<?php
/**
 * Author: Jesse
 * Email : jessedev@163.com
 * Date: 2024/6/14 23:17
 * FileName: NacosDriver.php
 * DESC: The Relentless Pursuit of Perfection.
 */

namespace Common\Kernel\Nacos;

use Hyperf\Collection\Arr;
use Hyperf\ConfigNacos\Constants;

/**
 * Nacos驱动
 */
class NacosDriver extends \Hyperf\ConfigNacos\NacosDriver
{
    /**
     * 更新配置
     *
     * @param array $config
     * @return void
     */
    protected function updateConfig(array $config): void
    {
        $root = $this->config->get('config_center.drivers.nacos.default_key');

        foreach ($config as $key => $conf) {
            if (is_int($key)) {
                $key = $root;
            }

            // 默认配置走全部
            if($root == $key && is_array($conf)) {
                foreach ($conf as $k => $sonConf) {
                    if (is_array($sonConf) && $this->config->get('config_center.drivers.nacos.merge_mode') === Constants::CONFIG_MERGE_APPEND) {
                        $sonConf = Arr::merge($this->config->get($k, []), $sonConf);
                    }
                    $this->config->set($k, $sonConf);
                }
            } else {
                if (is_array($conf) && $this->config->get('config_center.drivers.nacos.merge_mode') === Constants::CONFIG_MERGE_APPEND) {
                    $conf = Arr::merge($this->config->get($key, []), $conf);
                }
                $this->config->set($key, $conf);
            }
        }
    }
}